import React, {Component} from 'react';
import {
	Platform,
	StyleSheet,
	Image,
	Text,
	View,
	TextInput,
	PixelRatio,
	TouchableOpacity,
	Alert,
	AsyncStorage
} from 'react-native';

var Common = require('./common');
const pixelRatio = PixelRatio.get();
const dismissKeyboard = require('dismissKeyboard');
export default class Login extends Component<Props> {

	static navigationOptions = {
		// header: null,
	}

	constructor(props) {
		super(props);
		this.state = {
			phone: '',
			imgsrc: 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1514356844477&di=7c57fde0860194258cbec5c2720e0cf5&imgtype=0&src=http%3A%2F%2Fwww.swkweike.com%2Fdata%2Fuploads%2F2015%2F05%2F26%2F20430804665564348adc669.png',
			login_email: '',
			login_password:  ''
		};
	}

	login() {
		var _this = this;
		console.log(this.state.login_email);

		if(this.state.login_email == ''){
			Alert.alert('请填写手机号');
			return false;
		}

		if(this.state.login_password == ''){
			Alert.alert('请填写密码');
			return false;
		}
		var config = {
			method: 'POST',
			headers: {
				'Accept': 'application/json',
				'Content-Type': 'application/json',
				'credentials': 'include'
			},
			credentials: 'include'
		}
		var param = {verify: '08login', t: '', is_ajax: 1, login_email: this.state.login_email, login_password: this.state.login_password}
		if (param) {
			config["body"] = JSON.stringify(param);
		}

		fetch(Common.url + 'thinksns/index.php?app=public&mod=Passport&act=doAppAjaxLogin', config).then((response) =>{
			console.log(response.headers)
			AsyncStorage.setItem(
				'cookie',
				response.headers.map['set-cookie']
			);
			return response.json()
		} ).then((res) => {
			if (res.status == 1) {
				console.log(res)
				AsyncStorage.setItem(
					'logindata',
					JSON.stringify(res.data)
				);
				this.props.navigation.navigate('Mine');
			}
		})
		return;
		Common.ajaxPostLogin(Common.url + 'thinksns/index.php?app=public&mod=Passport&act=doAppAjaxLogin', function (res) {
			console.log(res)
			if (res.status == 1) {
				console.log(res)
				AsyncStorage.setItem(
					'logindata',
					JSON.stringify(res.data)
				);
				this.props.navigation.navigate('Mine');



				// AsyncStorage.setItem(
				// 	'uid',
				// 	res.data.uid,
				// 	(error)=>{
				// 		if (error){
				// 			alert('存值失败:',error);
				// 		}
				// 	}
				// );
				//
				// AsyncStorage.setItem(
				// 	'uname',
				// 	res.data.uname,
				// 	(error)=>{
				// 		if (error){
				// 			alert('存值失败:',error);
				// 		}
				// 	}
				// );
				//
				// AsyncStorage.setItem(
				// 	'mobile',
				// 	res.data.mobile,
				// 	(error)=>{
				// 		if (error){
				// 			alert('存值失败:',error);
				// 		}
				// 	}
				// );
				//
				// AsyncStorage.setItem(
				// 	'phone',
				// 	res.data.phone,
				// 	(error)=>{
				// 		if (error){
				// 			alert('存值失败:',error);
				// 		}
				// 	}
				// );
				//
				// AsyncStorage.setItem(
				// 	'realname',
				// 	res.data.realname,
				// 	(error)=>{
				// 		if (error){
				// 			alert('存值失败:',error);
				// 		}
				// 	}
				// );
				//
				// AsyncStorage.setItem(
				// 	'weixin',
				// 	res.data.weixin,
				// 	(error)=>{
				// 		if (error){
				// 			alert('存值失败:',error);
				// 		}
				// 	}
				// );
			}
		}.bind(this), {verify: '08login', t: '', is_ajax: 1, login_email: this.state.login_email, login_password: this.state.login_password});
	}

	getCode() {
		var time = new Date().getTime();
		this.setState({
			imgsrc: Common.url + 'tools/regcode.php?verify=08login&t=' + time

		})
	}

	toRegister() {
		this.props.navigation.navigate('Register')
	}

	componentWillMount() {
		this.getCode();
	}

	render() {
		return (
			<View style={styles.container}>
				<View style={styles.logo}>
					<Text>LOGO</Text>
				</View>
				<View style={styles.row}>
					<Image style={{width: 12, height: 17, marginRight: 20}} source={require('./images/iphone.png')}/>
					<TextInput
						style={styles.text}
						onChangeText={(text) => this.setState({
							login_email: text
						})}
						placeholder='请输入手机号'
						placeholderTextColor='#d9d9d9'
					/>
				</View>
				<View style={styles.row}>
					<Image style={{width: 13, height: 16, marginRight: 20}} source={require('./images/mima.png')}/>
					<TextInput
						style={styles.text}
						onChangeText={(text) => this.setState({
							login_password: text
						})}
						placeholder='请输入密码'
						placeholderTextColor='#d9d9d9'
					/>
				</View>

				<TouchableOpacity onPress={this.login.bind(this)} style={styles.btnLogin} activeOpacity={0.6}>
					<Text style={{color: '#fff'}}>登录</Text>
				</TouchableOpacity>
				<TouchableOpacity activeOpacity={1} onPress={this.toRegister.bind(this)}>
					<Text style={styles.tips}>还没有账号？<Text style={{color: '#3e79fe'}}>立即注册</Text></Text>
				</TouchableOpacity>
			</View>
		);
	}
}

const styles = StyleSheet.create({
	container: {
		flex: 1,
		height: Common.screenHeight,
		backgroundColor: '#fff',
	},
	logo: {
		paddingTop: 100,
		height: 200,
		justifyContent: 'center',
		alignItems: 'center'
	},
	row: {
		flexDirection: 'row',
		alignItems: 'center',
		paddingLeft: 36,
		borderBottomWidth: 1,
		borderBottomColor: '#f0f0f0'
	},
	text: {
		flex: 1,
		height: 54,
		color: '#333'
	},
	btnLogin: {
		height: 36,
		backgroundColor: '#3e79fe',
		marginRight: 38,
		marginLeft: 38,
		marginTop: 42,
		marginBottom: 38,
		borderRadius: 18,
		justifyContent: 'center',
		alignItems: 'center',
	},
	tips: {
		textAlign: 'center',
		fontSize: 14,
		color: '#a7a7a7'
	}
});
